package com.example.chasshelper;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.chasshelper.adapter.ViewPagerFragmentAdapter;
import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.fragment.ClassroomStuFragment;
import com.example.chasshelper.fragment.ClassroomTeaFragment;
import com.example.chasshelper.fragment.SubjectTeaFragment;
import com.example.chasshelper.fragment.TestTeaFragment;
import com.example.chasshelper.fragment.SettingFragment;
import com.example.chasshelper.fragment.SubjectStuFragment;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;
import java.util.List;

/*
* 需要往各个页面传递一个用户，Teacher或者Student
* */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = MainActivity.class.getSimpleName();
    private final Activity mContext = MainActivity.this;
    //控件
    private ViewPager2 vp2_content;
    private LinearLayout ll_classroom;
    private LinearLayout ll_course;
    private LinearLayout ll_test;
    private LinearLayout ll_setting;
    private ImageView iv_classroom;
    private ImageView iv_course;
    private ImageView iv_test;
    private ImageView iv_setting;
    private TextView tv_classroom;
    private TextView tv_course;
    private TextView tv_test;
    private TextView tv_setting;
    //当前页面对应的导航栏按钮
    private ImageView iv_current;
    private TextView tv_current;
    //用户
    private Teacher teacher;
    private Student student;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();
        initData();
    }

    /*
    * 界面初始化
    * */
    private void initUI() {
        //绑定控件
        vp2_content = findViewById(R.id.vp2_content);
        ll_classroom = findViewById(R.id.ll_classroom);
        ll_course = findViewById(R.id.ll_course);
        ll_test = findViewById(R.id.ll_test);
        ll_setting = findViewById(R.id.ll_setting);

        iv_classroom = findViewById(R.id.iv_classroom);
        iv_course = findViewById(R.id.iv_course);
        iv_test = findViewById(R.id.iv_test);
        iv_setting = findViewById(R.id.iv_setting);

        tv_classroom = findViewById(R.id.tv_classroom);
        tv_course = findViewById(R.id.tv_course);
        tv_test = findViewById(R.id.tv_test);
        tv_setting = findViewById(R.id.tv_setting);
        //点击事件
        iv_classroom.setOnClickListener(this);
        iv_course.setOnClickListener(this);
        iv_test.setOnClickListener(this);
        iv_setting.setOnClickListener(this);
    }

    /*
    * 数据初始化
    * */
    private void initData() {
        iv_classroom.setSelected(true);
        tv_classroom.setTextColor(Color.parseColor("#00BCD4"));
        iv_current = iv_classroom;
        tv_current = tv_classroom;
        //实现不同用户加载不同界面
        //获取用户数据,并传递用户数据给各个fragment
        List<Fragment> fragments = new ArrayList<>();
        if (getIntent().getStringExtra("identity").equals("教师")) {
            teacher = (Teacher) getIntent().getSerializableExtra("teacher");
            Bundle bundle = new Bundle();
            //传递自定义类型数据
            bundle.putSerializable("teacher",teacher);
            bundle.putSerializable("identity","教师");

            ClassroomTeaFragment classroomTeaFragment = new ClassroomTeaFragment();
            SubjectTeaFragment subjectTeaFragment = new SubjectTeaFragment();
            TestTeaFragment testTeaFragment = new TestTeaFragment();
            SettingFragment settingFragment = new SettingFragment();

            classroomTeaFragment.setArguments(bundle);
            subjectTeaFragment.setArguments(bundle);
            testTeaFragment.setArguments(bundle);
            settingFragment.setArguments(bundle);

            fragments.add(classroomTeaFragment);
            fragments.add(subjectTeaFragment);
            fragments.add(testTeaFragment);
            fragments.add(settingFragment);
        } else {
            student = (Student) getIntent().getSerializableExtra("student");
            Bundle bundle = new Bundle();
            //传递自定义类型数据
            bundle.putSerializable("student",student);
            bundle.putSerializable("identity","学生");

            ClassroomStuFragment classroomStuFragment = new ClassroomStuFragment();
            SubjectStuFragment subjectStuFragment = new SubjectStuFragment();
            TestTeaFragment testTeaFragment = new TestTeaFragment();
            SettingFragment settingFragment = new SettingFragment();

            classroomStuFragment.setArguments(bundle);
            subjectStuFragment.setArguments(bundle);
            testTeaFragment.setArguments(bundle);
            settingFragment.setArguments(bundle);

            fragments.add(classroomStuFragment);
            fragments.add(subjectStuFragment);
            fragments.add(testTeaFragment);
            fragments.add(settingFragment);
        }

        //导航栏与页面滑动
        //adapter
        ViewPagerFragmentAdapter viewPagerFragmentAdapter = new ViewPagerFragmentAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        vp2_content.setAdapter(viewPagerFragmentAdapter);
        vp2_content.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changePage(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    /*
    * 点击事件
    * 点击按钮，切换页面
    * */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_classroom:
                vp2_content.setCurrentItem(0);
                break;
            case R.id.iv_course:
                vp2_content.setCurrentItem(1);
                break;
            case R.id.iv_test:
                vp2_content.setCurrentItem(2);
                break;
            case R.id.iv_setting:
                vp2_content.setCurrentItem(3);
                break;
        }
    }

    /*
    * 滑动页面，改变按钮颜色
    * */
    private void changePage(int position) {
        iv_current.setSelected(false);
        tv_current.setTextColor(Color.BLACK);
        switch (position) {
            case 0:
                iv_classroom.setSelected(true);
                tv_classroom.setTextColor(Color.parseColor("#00BCD4"));
                iv_current = iv_classroom;
                tv_current = tv_classroom;
                break;
            case 1:
                iv_course.setSelected(true);
                tv_course.setTextColor(Color.parseColor("#00BCD4"));
                iv_current = iv_course;
                tv_current = tv_course;
                break;
            case 2:
                iv_test.setSelected(true);
                tv_test.setTextColor(Color.parseColor("#00BCD4"));
                iv_current = iv_test;
                tv_current = tv_test;
                break;
            case 3:
                iv_setting.setSelected(true);
                tv_setting.setTextColor(Color.parseColor("#00BCD4"));
                iv_current = iv_setting;
                tv_current = tv_setting;
                break;
        }
    }
}